<?php
class CiucontractsController extends AppController {

	var $name = 'Ciucontracts';

	function index() {
		$this->Ciucontract->recursive = 0;
        $controller='Ciucontract';
		$conditions=array();
		$id=0;
		if (!empty($this->data)) {
		
			$document=$this->data[$controller]['document'];
			$raw=$this->Ciucontract->Ciuuser->findByDocument($document);
			$id=$raw['Ciuuser']['id'];
			$conditions[$controller . ".ciuuser_id" ]= $id;
            $this->paginate[$controller]['conditions'] = $conditions; 
		}
		$this->set('ciucontracts', $this->paginate());
	}
	function indexteacher() {
                $this->Ciucontract->recursive = 0;
                $ciuuser_id    = $this->getConnectedUser();
                $controller='Ciucontract';
                $conditions=array();
                $conditions[$controller . ".ciuuser_id" ]= $ciuuser_id;
                $this->paginate[$controller]['conditions'] = $conditions; 
                $this->set('ciucontracts', $this->paginate());
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid ciucontract', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('ciucontract', $this->Ciucontract->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Ciucontract->create();
			if ($this->Ciucontract->save($this->data)) {
				$this->Session->setFlash(__('The ciucontract has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ciucontract could not be saved. Please, try again.', true));
			}
		}
		$ciuusers = $this->Ciucontract->Ciuuser->find('list',array('conditions'=>array(array('Ciuuser.ciuusertype_id'=>'2'),array('Ciuuser.ciuuserstate_id'=>'1'))));
		$this->set(compact('ciuusers'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid ciucontract', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Ciucontract->save($this->data)) {
				$this->Session->setFlash(__('The ciucontract has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The ciucontract could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Ciucontract->read(null, $id);
		}
		$ciuusers = $this->Ciucontract->Ciuuser->find('list',array('conditions'=>array(array('Ciuuser.ciuusertype_id'=>'2'),array('Ciuuser.ciuuserstate_id'=>'1'))));
		$this->set(compact('ciuusers'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for ciucontract', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Ciucontract->delete($id)) {
			$this->Session->setFlash(__('Ciucontract deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Ciucontract was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
        function getConnectedUserData(){
		App::import('Component', 'Session');
		$mySession = new SessionComponent();
		$userInfo = $mySession->read('Auth.Ciuuser');
		$username=$userInfo['username'];
		return $this->Ciucontract->Ciuuser->findByUsername($username);
	}

	function getConnectedUser(){
		$ciuuserData = $this->getConnectedUserData();
		return $ciuuserData['Ciuuser']['id'];
	}
}
